/*-----------------------------------------------------
 * 权限子系统-系统字典模块-单个字典管理
 * ---------------------------------------------------*/
jx.ready(function () {
    window.api = {
        grid: '/auth/dicdetails/grid',
        create: '/auth/dicdetails/create',
        edit: '/auth/dicdetails/edit',
        delete: '/auth/dicdetails/delete',
        details: '/auth/dicdetails/details',
        status: '/auth/dicdetails/status',
        export: '/auth/dicdetails/export',
        dnd: '/auth/dicdetails/dnd'
    }
    //定义变量
    var $grid = $('#grid'),$gridPanel,gridInstance;
    var dicCode = window.dicCode;
    var gridDialogWidth = $grid.data('dialogWidth');
    var gridDialogHeight = $grid.data('dialogHeight');
    var gridUrl = jx.apiUrl(api.grid);
    var gridCreateUrl = jx.apiUrl(api.create);
    var gridEditUrl = jx.apiUrl(api.edit);
    var gridDeleteUrl = jx.apiUrl(api.delete);
    var gridStatusUrl = jx.apiUrl(api.status);
    var gridDetailsUrl = jx.apiUrl(api.details);
    var gridExportUrl = jx.apiUrl(api.export);
    var gridDndUrl = jx.apiUrl(api.dnd);

    //初始化表格
    var initGrid = function () {
        //表格控件初始化参数
        gridInstance = $grid.jxtreegrid({
            url: gridUrl,
            dndUrl: gridDndUrl,
            onBeforeLoad: function (row, param) {
                param.dicCode = dicCode;
                return true;
            },
            onDblClickRow: function (row) {
                var id = gridInstance.getRowId(row);
                jx.detailsDialog({
                    title: '查看字典明细详细信息',
                    url: gridDetailsUrl,
                    params: {id: id}
                });
            },
            onLoadSuccess: function () {
                gridInstance.getDataBodyPanel().contextmenu({target: '#gridcmenu'});
            },
            onContextMenu: function (e, row) {
                if (row) {
                    var id = gridInstance.getRowId(row);
                    gridInstance.select(id);
                }
            }
        });
        $gridPanel = gridInstance.getPanel();
    };

    //初始化表格事件
    var initGridEvent = function () {
        $('#btn-createroot,#btn-cmcreateroot').click(function () {
            jx.dialog({
                title: '新增根字典明细',
                url: gridCreateUrl,
                params: {dicCode: dicCode},
                width: gridDialogWidth,
                height: gridDialogHeight
            });
        });
        $('#btn-create,#btn-cmcreate').click(function () {
            var parentId = gridInstance.getSelectedRowId() || 0;
            jx.dialog({
                title: '新增字典明细',
                url: gridCreateUrl,
                params: {dicCode: dicCode, parentId: parentId},
                width: gridDialogWidth,
                height: gridDialogHeight
            });
        });
        $('#btn-edit,#btn-cmedit').click(function () {
            if (!gridInstance.hasSelectedRow()) return;
            var id = gridInstance.getSelectedRowId();
            jx.dialog({
                title: '修改字典明细',
                url: gridEditUrl,
                params: {id: id},
                width: gridDialogWidth,
                height: gridDialogHeight
            });
        });
        $('#btn-delete,#btn-cmdelete').click(function () {
            if (!gridInstance.hasSelectedRow()) return;
            var id = gridInstance.getSelectedRowId();
            jx.delete({
                confirm: '注：如果有子节点则都会被删除，并且无法撤销，您确定要删除吗？',
                url: gridDeleteUrl,
                data: {id: id},
                success: function (result) {
                    gridInstance.reloadGridData();
                }
            });
        });
        $('#btn-export').click(function () {
            exportData();
        });

        $gridPanel.on('click', '.gridstatus', function () {
            var id = $(this).data('id');
            var val = $(this).data('val');
            setStatus(id, val);
        });
    };

    //导出数据
    var exportData = function () {
        jx.export(gridExportUrl, $.extend(jx.serialize($('#gridform')), {dicCode: dicCode}));
    };

    //设置数据状态
    var setStatus = function (id, val) {
        var status = val == '1' ? 0 : 1;
        jx.ajax({
            url: gridStatusUrl,
            data: {id: id, status: status},
            maskMsg: '正在更新状态,请稍等...',
            success: function (result) {
                gridInstance.reloadGridData();
                jx.toastr.success('状态更新成功');
            }
        });
    };

    //对外接口
    //刷新表格数据
    window.reloadGridData = function () {
        gridInstance.reloadGridData();
    }

    //初始化
    initGrid();
    initGridEvent();
});